﻿using CommonHelper;
using DataBaseLib.BLL;
using DataBaseLib.Entities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonProject_DeskTop
{
    public class DataCenter
    {
        static DataCenter _instance = new DataCenter();
        public static DataCenter Instance
        {
            get { return _instance; }
        }

        #region 全局属性

        #region 配置属性
        public string CurrentDB { get; set; }
        #endregion


        #endregion


        /// <summary>
        /// 用户逻辑层
        /// </summary>
        public StudentBLL studentBLL { get; set; }


        public bool Init()
        {
            try
            {
                LogHelper.logSysRun.Info("系统数据初始化开始...");
                studentBLL = new StudentBLL();
                //先更新配置文件,这里我们可以将程序新增的配置写在代码里面,程序更新后自动添加到配置文件
                UpdateConfig();
                ReadAppConfig();
                LogHelper.logSysRun.Info("系统数据初始化完成...");
            }
            catch (Exception ex)
            {
                LogHelper.logSysErr.Error("系统数据初始化异常"+ ex.ToString());
                return false;
            }
            return true;
        }

        private void ReadAppConfig()
        {
            CurrentDB = AppConfigHelper.SGS("DefaultDB");
        }

        private void UpdateConfig()
        {
            List<bool> results = new List<bool>();
            string path =$"{System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)}\\CommonProject_DeskTop.exe.config";
            results.Add(AppConfigHelper.AddAppSettingsNode(path, "DefaultDB", "mysql", "默认数据库配置"));
            //后续继续添加其他配置
            if (results.Contains(true))
            {
                AppConfigHelper.RefreshConfig();
            }
        }
    }
}
